package com.shuaiche.yeguan.ui.share;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.model.SCMultiShareSelectModel;
import com.shuaiche.yeguan.util.GlideUtil;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 类描述：多图分享时图片显示适配器
 *
 * @auther: baoyinbo
 * @date: 2018/12/27 下午2:20
 */

public class SCMultiCarPicsSelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int ITEM_NORMAL = 1;     //正常的图片显示
    public static final int ITEM_CAMERA = 2;     //加载本地图片选择

    private Context context;
    private List<SCMultiShareSelectModel> datas;
    private LayoutInflater mInflater;
    private Bitmap qrCodeBitmap;

    public SCMultiCarPicsSelectAdapter(Context mContext, List<SCMultiShareSelectModel> mDatas, Bitmap qrCodeBitmap) {
        this.context = mContext;
        this.datas = mDatas;
//        datas.add(new SCMultiShareSelectModel("", false, false));
        mInflater = LayoutInflater.from(mContext);
        this.qrCodeBitmap = qrCodeBitmap;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        if (viewType == ITEM_CAMERA) {
            view = mInflater.inflate(R.layout.sc_item_upload_picture_add_grid, parent, false);
            holder = new ViewHolderCAMERA(view);
        } else {
            view = mInflater.inflate(R.layout.sc_item_multi_share_select_grid, parent, false);
            holder = new ViewHolderPics(view);

        }
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolderCAMERA) {

        } else {

            final String path = datas.get(position).getUrl();

//            if (path.equals("sccache")) {
            if ("sccache".equals(path)) {
                ((ViewHolderPics)holder).image.setImageBitmap(qrCodeBitmap);
            } else {
                if (datas.get(position).isLocal()) {
                    //本地相册的图
                    GlideUtil.loadImg(path,
                            ((ViewHolderPics)holder).image
                    );
                } else {
                    //网络加载
                    GlideUtil.loadImg(context,
                            path,
                            ((ViewHolderPics)holder).image,
//                            R.mipmap.default_error);
                            R.mipmap.default_error);
                }
            }
            ((ViewHolderPics)holder).ivSelect.setSelected(datas.get(position).isSelect());
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callListener.itemClick(position);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return  ITEM_NORMAL;

    }

    public int getCount() {
        return  datas.size();
    }

    @Override
    public int getItemCount() {
        return datas != null ? datas.size() : 0;
    }


    /**
     * 列表
     */
    public static class ViewHolderPics extends RecyclerView.ViewHolder {
        ImageView image;
        ImageView ivSelect;
        public ViewHolderPics(View view){
            super(view);
            image = (ImageView) view.findViewById(R.id.imageView);
            ivSelect = (ImageView) view.findViewById(R.id.ivSelect);
        }
    }

    /**
     * 推荐
     */
    public static class ViewHolderCAMERA extends RecyclerView.ViewHolder {
        ImageView iv;
        public ViewHolderCAMERA(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv);

        }
    }

    public interface CallbackListener {
        void itemClick(int position);
    }

    /**回调 拨打电话 / 隐藏 监听*/
    public CallbackListener callListener;

    public void setCallbackListener (CallbackListener callListener) {
        this.callListener = callListener;
    }
}
